package com.wushi.lenovo.asia5b.app_ui.good.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.utils.DisplayUtils;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.ChargesBean;
import com.wushi.lenovo.asia5b.app_widget.span.CenterAlignImageSpan;

import java.util.List;

/**
 * Created by zhangyuncai on 2019/2/18.
 */
public class OrderCheckPriceAdapter extends BaseQuickAdapter<ChargesBean, BaseViewHolder> {

    public OrderCheckPriceAdapter(@Nullable List<ChargesBean> data) {
        super(R.layout.item_ordercheck_price_adapter, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChargesBean item) {
        TextView tv_ordercheck_title = helper.getView(R.id.tv_ordercheck_title);
        if (!TextUtils.isEmpty(item.getFormula())) {
            addSpan(tv_ordercheck_title, item);
        } else {
            tv_ordercheck_title.setText(item.getDefault_title() + ":");
        }

        helper.setText(R.id.tv_ordercheck_price, item.getText());
        helper.setGone(R.id.tv_ordercheck_remove, TextUtils.equals(item.getCode(), "voucher"));
        helper.addOnClickListener(R.id.tv_ordercheck_remove);
    }

    /**
     * 如果需要添加icon提示图片
     *
     * @param tv_ordercheck_title
     * @param item
     */
    private void addSpan(TextView tv_ordercheck_title, ChargesBean item) {
        //注意此处showText后+ " "主要是为了占位
        SpannableString titleSpan = new SpannableString(item.getDefault_title() + ":  ");
        int len = titleSpan.length();
        //图片
        Drawable d = ContextCompat.getDrawable(mContext, (R.drawable.ic_notice));
        d.setBounds(0, 0, DisplayUtils.dip2px(mContext, 15), DisplayUtils.dip2px(mContext, 15));
        //构建ImageSpan
        CenterAlignImageSpan span = new CenterAlignImageSpan(d);
        titleSpan.setSpan(span, len - 1, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSpan.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                ToastUtil.showToast(item.getFormula());
            }
        }, len - 1, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_ordercheck_title.setText(titleSpan);
        tv_ordercheck_title.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
